package com.kk;

import com.kk.utils.AdvanceThreadPool;

/**
 * the type of advance
 *
 * <p>
 * .
 *
 * @author 王志强
 * @date 2022-03-31
 */
public class HeroGaiLun {
    String name;
    int hp;

    int minHp = 0;
    int maxHp = 10;

    public HeroGaiLun(String name, Integer hp) {
        this.name = name;
        this.hp = hp;
    }

    @SuppressWarnings("InfiniteLoopStatement")
    public static void main(String[] args) {

        HeroGaiLun gl = new HeroGaiLun("盖伦", 0);

        //1 减血线程
        AdvanceThreadPool.poolExecutor.submit(() -> {
            while (true) {
                gl.hurt();
            }
        });

        //2 加血线程
        AdvanceThreadPool.poolExecutor.submit(() -> {
            while (true) {
                gl.recover();
            }
        });

        //3 死亡线程
        AdvanceThreadPool.poolExecutor.submit(() -> {
            while (true) {
                gl.die();
            }
        });

        //4 出生线程
        AdvanceThreadPool.poolExecutor.submit(() -> {
            while (true) {
                gl.born();
            }
        });
    }

    /**
     * 减血
     * @throws InterruptedException
     */
    public synchronized void hurt() throws InterruptedException {
        if (hp == minHp) {
            this.notifyAll();
            System.out.println("--等待,唤醒加血");
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


        hp -= 1;
        System.out.println("减血: " + hp);
        Thread.sleep(50);
    }

    /**
     * 加血
     * @throws InterruptedException
     */
    public synchronized void recover() throws InterruptedException {
        hp += 1;
        System.out.println("recover after hp: " + hp);
        Thread.sleep(500);

        if (hp == maxHp) {
            this.notifyAll();
            System.out.println("唤醒减血,++等待");
            this.wait();
        }
    }

    /**
     * 空血死亡
     * @throws InterruptedException
     */
    public synchronized void die() throws InterruptedException {
        wait();
        if (hp == 0) {
            System.out.println("你空血死啦!");
        }
    }


    /**
     * 满血出生
     * @throws InterruptedException
     */
    public synchronized void born() throws InterruptedException {
        wait();
        if (hp == 10) {
            System.out.println("你满血复活啦!");
        }
    }
}
